package org.mspring.platform.denglu;

import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.NameValuePair;

/**
 *
 * @author Gao Youbo
 * @since 2014-05-26 16:06:57
 */
public class DengluUtils {

    /**
     * URL签名
     *
     * @param nvps
     *            参数列表
     * @return
     */
    public static final String sign(List<NameValuePair> nvps) {
        StringBuilder sb = new StringBuilder();
        if (nvps != null && !nvps.isEmpty()) {
            for (NameValuePair nv : nvps) {
                sb.append(nv.getName()).append("=").append(nv.getValue());
            }
        }
        sb.append(Constants.APIKEY);
        return DigestUtils.md5Hex(sb.toString());
    }

    /**
     * URL签名
     *
     * @param params
     * @return
     */
    public static final String sign(Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> nv : params.entrySet()) {
                sb.append(nv.getKey()).append("=").append(nv.getValue());
            }
        }
        sb.append(Constants.APIKEY);
        return DigestUtils.md5Hex(sb.toString());
    }
}
